//
//  LTVisitorView.swift
//  LTWeibo
//
//  Created by mac on 16/6/25.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit


class LTVisitorView: UIView {
    
    
    var closure: (()->())?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
       settingUIView()
        
       
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func settingUIView() {
        
        backgroundColor = rgb(237, green: 237, blue: 237)
    
        addSubview(animateImage)
        addSubview(maskImage)
        addSubview(centerImage)
        addSubview(desLabel)
        addSubview(loginButton)
        addSubview(registButton)
        
        
        //为子控件添加约束
        animateImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        centerImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        desLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImage.snp_bottom).offset(16)
            make.centerX.equalTo(maskImage)
            make.width.equalTo(224)
            make.height.equalTo(36)
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(desLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(desLabel)
            
        }
        
        registButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(desLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(desLabel)
            
        }
        
   
    
    }
    
    
    //转动图片
//    private lazy var animateImage: UIImage = UIImage(named: "visitordiscover_feed_image_house")!
    
    private lazy var animateImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //中间图片
    private lazy var centerImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    //文字Label 
    private lazy var desLabel: UILabel = {
    
        let dLabel = UILabel()
 
        dLabel.text = "关注一些人,回这里看看有什么惊喜 关注一些人,回这里看看有什么惊喜"
        dLabel.numberOfLines = 0
        dLabel.textAlignment = .Center
        dLabel.textColor = ThemeColor
        dLabel.font = UIFont.systemFontOfSize(14.0)
     
        return dLabel
    
    
    }()
    
    
    //登录按钮
    private lazy var loginButton: UIButton = {
    
        let lButton = UIButton()
        
        lButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        lButton.setTitle("登录", forState: UIControlState.Normal)
        lButton.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        lButton.titleLabel?.font = UIFont.systemFontOfSize(14.0)
        
        //给按钮添加点击事件
        lButton.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
    
        return lButton
    
    
    }()
    
    
    

    
    
    //注册按钮
    private lazy var registButton: UIButton = {
    
        let rButton = UIButton()
        
        rButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        rButton.setTitle("注册", forState: UIControlState.Normal)
        rButton.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        rButton.titleLabel?.font = UIFont.systemFontOfSize(14.0)
        
        //给按钮添加点击事件
        rButton.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
      
        return rButton
    }()
    
    
    //定义一个供外界调用的方法,并设置参数
    func changeView(imageName: String?,title: String?) {
        
        if let imgName = imageName, texttitle = title {
            
            centerImage.image = UIImage(named: imgName)
            desLabel.text = texttitle
            animateImage.hidden = true
            
        }else {
            
            settingAnim()
            
        }
        
        
        
        
    }
    
   //设置图片旋转为基本类型旋转
    func settingAnim() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 5
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        animateImage.layer.addAnimation(anim, forKey: nil)
        
    
    }
    
    
    
    //事件按钮点击事件
    
   @objc private func btnClick() {
    
        closure?()
    
    }
    
    
    

}
